#!/bin/bash
#创建统计日期

#输入日期的格式 
inputDateFormat="+%Y-%m-%d"
#系统名，用于判断是linux 还是mac,然后分别调用不同的date参数
uname1=$(uname)
#当前shell脚本路径
CUR_DIR=$(cd "$(dirname "${BASH_SOURCE-$0}")"; pwd)
#输出日期的路径
OUT_DATE_PROP_FILE="${CUR_DIR}/../outdate.properties"

# 日期输出格式，可根据不同的要求，修改输出日期的格式
export handleDateFormat="+%Y%m%d"
#export handleDateFormat="+%Y-%m-%d"


# 相关的工具函数 start------
monthStartDay(){
    inputDay=$1
    outDateFormat=$2
    #当前日期是当月的第几天
    if [ 'Linux' = "${uname1}" ];then
      	  month_of_day0=$(date -d "$inputDay"  "+%d")
    else
          #month_of_day0=$(date  -j -f ${inputDateFormat//+/} "$inputDay" +%d)
          month_of_day0=$(date -d "$inputDay"  "+%d")
	fi
    month_of_day=`expr $month_of_day0 - 1`
    echo $(dateAddDay ${inputDay} ${outDateFormat} -${month_of_day} )
}
monthEndDay(){
    inputDay=$1
    outDateFormat=$2
    if [ 'Linux' = "${uname1}" ];then
      	  month_start_date1=$(monthStartDay "$inputDay"  "$outDateFormat" '')
          next_month_day=$(date -d "$month_start_date1 +1month" ${inputDateFormat})
    else
          #next_month_day=$(date  -v +1m -j -f ${inputDateFormat//+/} "$inputDay" ${inputDateFormat})
          month_start_date1=$(monthStartDay "$inputDay"  "$outDateFormat" '')
          next_month_day=$(date -d "$month_start_date1 +1month" ${inputDateFormat})
	fi
    next_month_start_day=$(monthStartDay "$next_month_day"  "$inputDateFormat" '')
    echo $(dateAddDay ${next_month_start_day} ${outDateFormat} -1 )
}

weekStartDay(){
    inputDay=$1
    outDateFormat=$2
     #当前日期是周几
    if [ 'Linux' = "${uname1}" ];then
      	  week_of_day0=$(date -d "$inputDay"  "+%w")
    else
          #week_of_day0=$(date  -j -f ${inputDateFormat//+/} "$inputDay" +%w)
          week_of_day0=$(date -d "$inputDay"  "+%w")
	fi
	
	if [ '0' = "${week_of_day0}" ];then
	    week_of_day=`expr $week_of_day0 + 6`
	else
	    week_of_day=`expr $week_of_day0 - 1`
	fi
    echo $(dateAddDay ${inputDay} ${outDateFormat} -${week_of_day} )
}
weekEndDay(){
    inputDay=$1
    outDateFormat=$2
    #周一的日期加6天就是周日的日期
    week_start_day=$(weekStartDay "$inputDay"  "$inputDateFormat" '')
    echo $(dateAddDay ${week_start_day} ${outDateFormat} +6 )
}
dateAddDay(){
	 inputDay=$1
	 outDateFormat=$2
	 addDays=$3
	if [ 'Linux' = "$uname1" ];then
	   [ -n "${addDays}" ] &&  addDaysStr=" ${addDays} days"
       outDay=$(date -d "$inputDay ${addDaysStr}" ${outDateFormat})
    else
	  #[ -n "${addDays}" ] &&  addDaysStr="  -v ${addDays}d "
	  #outDay=$(date ${addDaysStr}   -d -f ${inputDateFormat//+/} "$inputDay" ${outDateFormat})
	  [ -n "${addDays}" ] &&  addDaysStr=" ${addDays} days"
       outDay=$(date -d "$inputDay ${addDaysStr}" ${outDateFormat})
    fi
    echo ${outDay}
}
# 相关的工具函数 end------


#如果第一个参数未传日期，则取昨天的日期，输入日期的格式是+%Y-%m-%d
if [ -z $1 ];then
	uname1=$(uname)
	if [ 'Linux' = $uname1 ];then
      inputDay=$(date -d "1 days ago" ${inputDateFormat})
    else
	  #inputDay=$(date -v -1d ${inputDateFormat})
	  inputDay=$(date -d "1 days ago" ${inputDateFormat})
    fi
else
  inputDay=$1
fi


#要处理的日期
export day_date=$(dateAddDay "$inputDay"  "$handleDateFormat" '')
#周一的日期
export week_start_date=$(weekStartDay "$inputDay"  "$handleDateFormat" '')
#周日的日期
export week_end_date=$(weekEndDay "$inputDay"  "$handleDateFormat" '')
#月开始日期
export month_start_date=$(monthStartDay "$inputDay"  "$handleDateFormat" '')
#月结束日期
export month_end_date=$(monthEndDay "$inputDay"  "$handleDateFormat" '')

echo "-------------"
echo "inputDay:${inputDay}"
echo "day_date:${day_date}"
echo "week_start_date:${week_start_date}"
echo "week_end_date:${week_end_date}"
echo "month_start_date:${month_start_date}"
echo "month_end_date:${month_end_date}"
echo "-------------"

#azakaban 输出日期参数
if [ -n "${JOB_OUTPUT_PROP_FILE}" ];then
 echo '{"day_date" : "'"$day_date"'",
"week_start_date" : "'"$week_start_date"'",
"week_end_date" : "'"$week_end_date"'",
"month_start_date" : "'"$month_start_date"'",
"month_end_date" : "'"$month_end_date"'"}' > "${JOB_OUTPUT_PROP_FILE}"
echo "output date  to ${JOB_OUTPUT_PROP_FILE} .."
fi

#将统计日期输出到文件，以方便脚本加载
echo ${uname1}
echo "day_date=${day_date}" >"${OUT_DATE_PROP_FILE}"
echo "week_start_date=${week_start_date}" >>"${OUT_DATE_PROP_FILE}"
echo "week_end_date=${week_end_date}" >>"${OUT_DATE_PROP_FILE}"
echo "month_start_date=${month_start_date}" >>"${OUT_DATE_PROP_FILE}"
echo "month_end_date=${month_end_date}" >>"${OUT_DATE_PROP_FILE}"
